package com.wlh.animation.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

/**
 * Created by weilh on 2017/7/3.
 * 可控制最大显示个数的ListView, 超过最大显示个数则变成滑动，， <=0 表示不做处理
 */

public class MaxItemListView extends ListView{

    private int mMaxItemCount;
    public MaxItemListView(Context context) {
        super(context);
    }

    public MaxItemListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MaxItemListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMaxItemCount(int count) {
        this.mMaxItemCount = count;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getAdapter() != null && mMaxItemCount > 0 && getAdapter().getCount() > mMaxItemCount) {
            View child0 = getChildAt(0);
            if (child0 != null) {
                int childHeight = child0.getMeasuredHeight();
                setMeasuredDimension(getMeasuredWidth(), childHeight * mMaxItemCount);
            }
        }
    }
}
